﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.OptionForms
{
    public partial class UserPropertyForm : Form
    {
        Connection conn;
        User user;
        private bool create = false;

        /// <summary>
        /// Constructor to create new User
        /// </summary>
        /// <param name="_conn">Connection</param>
        public UserPropertyForm(Connection _conn)
        {
            InitializeComponent();
            this.conn = _conn;
            create = true;
            this.Text = "Create new User";
        }

        /// <summary>
        /// Constructor to show User properties and modify it
        /// </summary>
        /// <param name="_conn">Connection</param>
        /// <param name="_user">User to show and modify</param>
        public UserPropertyForm(Connection _conn, User _user)
        {
            InitializeComponent();
            this.conn = _conn;
            user = _user;
            userTextBox.Text = user.Name;
            hostTextBox.Text = user.Host;
            passTextBox1.Text = "*passwordUnChanged";
            passTextBox2.Text = "*passwordUnChanged";
            create = false;
            this.Text = "Edit User - '"+user.ToString()+"'";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (passTextBox1.Text.Equals(passTextBox2.Text))
            {
                //try
                //{
                    if (create)
                    {
                        user = new User(conn, userTextBox.Text, hostTextBox.Text);
                        user.create(passTextBox1.Text);
                        MessageBox.Show(this, "User '"+user.ToString()+"' created successful !", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ((UserManagerForm)this.Owner).refreshUserList();
                        this.Dispose(); 
                    }
                    else // update user properties
                    {
                        user.update(userTextBox.Text, hostTextBox.Text, passTextBox1.Text.Equals("*passwordUnChanged") ? "" : passTextBox1.Text);
                        ((UserManagerForm)this.Owner).refreshUserList();
                        this.Dispose();
                    }
                //}
                //catch (Exception ex)
                //{
                //     MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //}
            }
        }

        private void userTextBox_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = (!userTextBox.Text.Equals("") && !hostTextBox.Text.Equals(""));
        }

        private void hostTextBox_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = (!userTextBox.Text.Equals("") && !hostTextBox.Text.Equals(""));
        }

        private void passTextBox1_Click(object sender, EventArgs e)
        {
            passTextBox1.SelectAll();
        }

        private void passTextBox2_Click(object sender, EventArgs e)
        {
            passTextBox2.SelectAll();
        }
    }
}
